package tt.dz.biz.dao.merch;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Repository;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.common.LogUtil;
import tt.dz.vo.BalanceCheckVO;
import tt.dz.vo.BorrowLoanCheckVO;

@Repository("balanceCheckDAO")
public class BalanceCheckDAOImp
  extends BaseDAO
{
  private static final String INSERT_BALANCE_CHECK = "insert into ms_balance_check(merchant_code,sub_subject_code,daily_amount,subject_amount,cur_bal,accounting_date,remark)  values(:merchantCode,:subSubjectCode,:dailyAmount,:subjectAmount,:curBal,:accountingDate,:remark )";
  public static final String QUERY_MS_BALANCE_CHECK = "select  sf.merchant_code,sf.sub_subject_code,sf.daily_amount,sf.subject_amount,sf.cur_bal,sf.accounting_date,sf.remark  from ms_balance_check as sf ";
  public static final String DELETE_MS_BALANCE_CHECK_BY_DATE = "delete from ms_balance_check  where  accounting_date=:accountingDate ";
  public static final String QUERY_MS_BORROW_LOAN_CHECK = "select  sf.borrow_amount,sf.loan_amount,sf.is_bal,sf.accounting_date,sf.plat_time,sf.remark  from ms_borrow_loan_check as sf ";
  public static final String DELETE_MS_BORROW_LOAN_CHECK_BY_DATE = "delete from ms_borrow_loan_check  where  accounting_date=:accountingDate ";
  public static final String INSERT_MS_BORROW_LOAN_CHECK = "insert into ms_borrow_loan_check(borrow_amount,loan_amount,is_bal,accounting_date,plat_time,remark)  values(:borrowAmount,:loanAmount,:isBal,:accountingDate,CURRENT_TIMESTAMP,:remark )";
  public static final String QUERY_BORROW_LOAN_AMOUNT_DATE = "select   sum(case when loan_flag = 0 then amount else 0 end )   as borrowAmount ,\tsum(case when loan_flag = 1 then amount else 0 end ) as  loanAmount    from  ms_merch_account_detail  where accounting_date=:accountingDate";
  
  public int insertBalanceCheck(BalanceCheckVO balanceCheckVo)
  {
    return save("insert into ms_balance_check(merchant_code,sub_subject_code,daily_amount,subject_amount,cur_bal,accounting_date,remark)  values(:merchantCode,:subSubjectCode,:dailyAmount,:subjectAmount,:curBal,:accountingDate,:remark )", balanceCheckVo);
  }
  
  public int[] insertBatchBalanceCheck(List<BalanceCheckVO> balanceCheckList)
  {
    return batchSave("insert into ms_balance_check(merchant_code,sub_subject_code,daily_amount,subject_amount,cur_bal,accounting_date,remark)  values(:merchantCode,:subSubjectCode,:dailyAmount,:subjectAmount,:curBal,:accountingDate,:remark )", balanceCheckList);
  }
  
  public List<BalanceCheckVO> queryBalanceCheckByAccoutingDate(Date accountingDate)
    throws Exception
  {
    try
    {
      BalanceCheckVO param = new BalanceCheckVO();
      param.setAccountingDate(accountingDate);
      StringBuilder sql = new StringBuilder("select  sf.merchant_code,sf.sub_subject_code,sf.daily_amount,sf.subject_amount,sf.cur_bal,sf.accounting_date,sf.remark  from ms_balance_check as sf ");
      sql.append("  where sf.accounting_date=:accountingDate ");
      return queryForList(sql.toString(), param, BalanceCheckVO.class);
    }
    catch (Exception e)
    {
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      LogUtil.error("通过账户日期查询发生额试算平衡数据异常，参数accountingDate:" + df.format(accountingDate), e);
      throw e;
    }
  }
  
  public int deleteBalanceCheckByAccoutingDate(Date accountingDate)
    throws Exception
  {
    try
    {
      BalanceCheckVO param = new BalanceCheckVO();
      param.setAccountingDate(accountingDate);
      return save("delete from ms_balance_check  where  accounting_date=:accountingDate ", param);
    }
    catch (Exception e)
    {
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      LogUtil.error("删除指定账务日期发生额试算平衡数据异常，参数accountingDate:" + df.format(accountingDate), e);
      throw e;
    }
  }
  
  public List<BorrowLoanCheckVO> queryBorrowLoanCheckByAccoutingDate(Date accountingDate)
    throws Exception
  {
    try
    {
      BorrowLoanCheckVO param = new BorrowLoanCheckVO();
      param.setAccountingDate(accountingDate);
      StringBuilder sql = new StringBuilder("select  sf.borrow_amount,sf.loan_amount,sf.is_bal,sf.accounting_date,sf.plat_time,sf.remark  from ms_borrow_loan_check as sf ");
      sql.append("  where sf.accounting_date=:accountingDate ");
      return queryForList(sql.toString(), param, BorrowLoanCheckVO.class);
    }
    catch (Exception e)
    {
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      LogUtil.error("通过账户日期查询借贷关系试算平衡数据异常，参数accountingDate:" + df.format(accountingDate), e);
      throw e;
    }
  }
  
  public int deleteBorrowLoanCheckByAccoutingDate(Date accountingDate)
    throws Exception
  {
    try
    {
      BorrowLoanCheckVO param = new BorrowLoanCheckVO();
      param.setAccountingDate(accountingDate);
      return save("delete from ms_borrow_loan_check  where  accounting_date=:accountingDate ", param);
    }
    catch (Exception e)
    {
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      LogUtil.error("删除指定账务日期的借贷关系试算平衡数据异常，参数accountingDate:" + df.format(accountingDate), e);
      throw e;
    }
  }
  
  public int insertBorrowLoanCheck(BorrowLoanCheckVO param)
    throws Exception
  {
    try
    {
      return save("insert into ms_borrow_loan_check(borrow_amount,loan_amount,is_bal,accounting_date,plat_time,remark)  values(:borrowAmount,:loanAmount,:isBal,:accountingDate,CURRENT_TIMESTAMP,:remark )", param);
    }
    catch (Exception e)
    {
      LogUtil.error("插入借贷试算平衡表异常，参数BorrowLoanCheckVO:" + param, e);
      throw e;
    }
  }
  
  public BorrowLoanCheckVO queryBorrowAndLoanAccoutingByDate(Date accountingDate)
    throws Exception
  {
    try
    {
      BorrowLoanCheckVO param = new BorrowLoanCheckVO();
      param.setAccountingDate(accountingDate);
      return (BorrowLoanCheckVO)queryForObject("select   sum(case when loan_flag = 0 then amount else 0 end )   as borrowAmount ,\tsum(case when loan_flag = 1 then amount else 0 end ) as  loanAmount    from  ms_merch_account_detail  where accounting_date=:accountingDate", param, BorrowLoanCheckVO.class);
    }
    catch (EmptyResultDataAccessException e)
    {
      BorrowLoanCheckVO vo = new BorrowLoanCheckVO();
      vo.setBorrowAmount(BigDecimal.valueOf(0L));
      vo.setLoanAmount(BigDecimal.valueOf(0L));
      return vo;
    }
    catch (Exception e)
    {
      LogUtil.error("计算指定账务日期的借和贷的金额异常，参数accountingDate:" + accountingDate, e);
      throw e;
    }
  }
}
